<?php

namespace App\Admin\Controllers\Message;

use App\Jobs\SendMessage;
use App\Models\Message;
use App\Models\MessageReceiver;
use Dcat\Admin\Admin;
use Dcat\Admin\Controllers\AdminController;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class MessageController extends AdminController
{

    public $title = '消息管理';


    /**
     * 列表
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Message(), function (Grid $grid){
            $grid->id;
            $grid->column('title', '标题');
            $grid->column('content', '内容');
//            $grid->column('type', '类型')->display(function ($type){
//                return Message::TYPE[$type];
//            });
            $grid->column('publish_at', '发布时间');
            $grid->column('receiver_type', '接收者类型')->display(function ($receiver_type){
                return Message::RECEIVER_TYPE[$receiver_type];
            });
            $grid->updated_at;
            $grid->created_at;
            $grid->actions(function ($action) {
                $action->disableEdit();
            });

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('receiver_type', '接收者类型')->select(Message::RECEIVER_TYPE);
            });
        });
    }


    /**
     * 表单
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Message());
        //$form->radio('type', '消息类型')->options(Message::TYPE)->default(1);
        $form->hidden('type')->default(1);
        $form->divider();//普通消息
        $form->select('receiver_type', '推送对象')->options(Message::RECEIVER_TYPE)->rules('required');
        $form->datetime('publish_at', '推送时间')->format('YYYY-MM-DD HH:mm')->rules('required');
        $form->text('title', '消息标题')->rules('required|max:100');
        $form->text('content', '消息内容')->rules('required');
        $form->text('link', '跳转连接');

//        $form->divider();// todo 图文消息
//        $form->select('receiver_type', '推送对象')->options(Message::RECEIVER_TYPE)->rules('required')->hideInDialog(false)->addElementClass('type2');
//        $form->text('title', '消息标题')->rules('required|max:100')->display(false)->addElementClass('type2');
//        $form->editor('content', '消息内容')->rules('required')->display(false)->addElementClass('type2');
//        $form->datetime('publish_at', '推送时间')->format('YYYY-MM-DD HH:mm')->rules('required')->display(false)->addElementClass('type2');
        //Admin::script('$(document.body).append(`<script src="/admin/js/message_form.js">`);');

        return $form;
    }


    /**
     * 详情
     * @param $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show($id, new Message());
        $show->id;
        $show->field('type', '消息类型')->as(function ($type) {
            return Message::TYPE[$type];
        });
        $show->field('receiver_type', '推送对象')->as(function ($receiver_type) {
            return Message::RECEIVER_TYPE[$receiver_type];
        });
        $show->field('title', '消息标题');
        $show->field('content', '消息内容');
        $show->field('link', '跳转连接');
        $show->field('publish_at', '发布时间');
        $show->updated_at;
        $show->created_at;
        $show->disableEditButton();

        return $show;
    }


    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $message_info = Message::find($id)->toArray();
        if(strtotime($message_info['publish_at']) <time()) {
            return response()->json(['status'=> false, 'message'=> '删除失败！该消息已发布']);
        }

        DB::beginTransaction();
        $re_del_receiver = MessageReceiver::query()->where('message_id', $id)->delete();
        if($re_del_receiver === false) {
            return response()->json(['status'=> false, 'message'=> '服务器繁忙！']);
        }
        $re_del_message = Message::destroy($id);
        if(!$re_del_message) {
            DB::rollBack();
            return response()->json(['status'=> false, 'message'=> '服务器繁忙！']);
        }
        DB::commit();
        return response()->json(['status'=> true, 'message'=> '删除成功']);

    }


}
